package com.zhihuichongcheng.application.ui.main.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.smartlib.cmnObject.util.GsonUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseFragment;
import com.zhihuichongcheng.application.bean.cricle.CricleCountBean;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.cricle.activity.CircleHealthyActivity;
import com.zhihuichongcheng.application.ui.cricle.activity.MyCricleActivity;
import com.zhihuichongcheng.application.ui.main.adapter.CricleCountAdapter;
import com.zhihuichongcheng.application.ui.mine.UserInfoActivity;

import java.util.HashMap;
import java.util.List;


/**
 * 朋友圈首页
 *
 * Created by 3water on 2016/10/26.
 */

public class CircleFragment extends BaseFragment implements View.OnClickListener {
    private ImageView mImImage;
    private TextView mTvTitle, mTvRight;
    private UserBean mUser;
    private ImageView mImUser;
    private CricleCountAdapter mAdapter;
    private XRecyclerView mRcvCricleCount;

    @Override
    protected void init() {
        super.init();
        initview();
        initData();
    }

    private void initview() {
        mRcvCricleCount = (XRecyclerView) mView.findViewById(R.id.rcv_cricleCount);
        mRcvCricleCount.setLayoutManager(new LinearLayoutManager(mContext));
        View view = LayoutInflater.from(mContext).inflate(R.layout.cricle_headview, mRcvCricleCount, false);
        mRcvCricleCount.addHeaderView(view);
        mRcvCricleCount.refreshComplete();
        mAdapter = new CricleCountAdapter(mContext);
        mAdapter.setOnItemOnclick(new OnItemOnclick<CricleCountBean>() {
            @Override
            public void onClick(CricleCountBean cricleCountBean, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("title", cricleCountBean.getCategoryName());
                bundle.putString("type", cricleCountBean.getJinbaoHomeCategoryId());
                startActivity(CircleHealthyActivity.class, bundle);
            }
        });
        mRcvCricleCount.setAdapter(mAdapter);
        mTvRight = (TextView) mView.findViewById(R.id.tv_right);
        mTvTitle = (TextView) mView.findViewById(R.id.tv_title);
        mImUser = (ImageView) mView.findViewById(R.id.im_user);
        mImImage = (ImageView) mView.findViewById(R.id.im_image);
        mImUser.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mRcvCricleCount.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData();
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    private void initData() {
        mUser = app.getUser();
        mTvTitle.setText("崇诚家园");
    }

    private void loadData() {
        HttpManager manager = HttpManager.getInstance(mContext);
        HashMap<String, String> params = new HashMap<>();
        params.put("token", mUser.getToken());
        manager.post(NetworkAPI.CricleCount, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                List<CricleCountBean> list = GsonUtil.paraeFromStringToList(value, CricleCountBean.class);
                mAdapter.setData(list);
                mAdapter.notifyDataSetChanged();
                mRcvCricleCount.refreshComplete();
                mRcvCricleCount.setNoMore(true);
            }

            @Override
            public void onFailure(String value) {
                mRcvCricleCount.refreshComplete();
                mRcvCricleCount.setNoMore(true);
            }
        }, params);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cricle;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_user:

                startActivity(UserInfoActivity.class, null);
                break;
            case R.id.tv_right:
                startActivity(MyCricleActivity.class, null);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }
}
